enum DIRLIST {
    //% block="北风"
    0,
    //% block="东北风"
    1
    //% block="东风"
    2,
    //% block="东南风"
    3
    //% block="南风"
    4,
    //% block="西南风"
    5    
    //% block="西风"
    6,
    //% block="西北风"
    7
}


//% color="#5ba55b" iconWidth=50 iconHeight=40
namespace usart_hmi { 

    //% block="HMI串口智能屏 接收(R)管脚#[RXPIN] 发射(T)管脚#[TXPIN] 切换到页面[PAGE]" blockType="command"
    //% RXPIN.shadow="dropdown" RXPIN.options="PIN_DigitalWrite" 
    //% TXPIN.shadow="dropdown" TXPIN.options="PIN_DigitalWrite"
    //% PAGE.shadow="normal" PAGE.defl="main" 
    export function HMI_PAGE(parameter: any, block: any) {
        let rxpin = parameter.RXPIN.code;
        let txpin = parameter.TXPIN.code;
        let page = parameter.PAGE.code;
        Generator.addInclude('include_SoftwareSerial_h', '#include "SoftwareSerial.h"');
        Generator.addObject(`char_hexEND`, `char `,  `hexEND[3]={0xff,0xff,0xff};`);
        Generator.addObject(`SoftwareSerial_hmi`, `SoftwareSerial`,  `hmi(${rxpin},${txpin});`);
        Generator.addSetup(`hmi_begin`, `hmi.begin(9600);`);
        Generator.addCode(`hmi.print("page ${page}");\n  hmi.write(hexEND,3);`);
    }   
    //% block="HMI串口智能屏 接收(R)管脚#[RXPIN] 发射(T)管脚#[TXPIN] 赋值[PAGE]的对象[OBJ]的文本[TXT]" blockType="command"
    //% RXPIN.shadow="dropdown" RXPIN.options="PIN_DigitalWrite" 
    //% TXPIN.shadow="dropdown" TXPIN.options="PIN_DigitalWrite"
    //% PAGE.shadow="normal" PAGE.defl="main" 
    //% OBJ.shadow="normal" OBJ.defl="t0" 
    //% TXT.shadow="normal" TXT.defl=\"hello\"
    export function HMI_PAGE_TXT(parameter: any, block: any) {
        let rxpin = parameter.RXPIN.code;
        let txpin = parameter.TXPIN.code;
        let page = parameter.PAGE.code;
        let obj = parameter.OBJ.code;
        let txt = parameter.TXT.code;
        Generator.addInclude('include_SoftwareSerial_h', '#include "SoftwareSerial.h"');
        Generator.addObject(`char_hexEND`, `char `,  `hexEND[3]={0xff,0xff,0xff};`);
        Generator.addObject(`SoftwareSerial_hmi`, `SoftwareSerial`,  `hmi(${rxpin},${txpin});`);
        Generator.addSetup(`hmi_begin`, `hmi.begin(9600);`);
        Generator.addCode(`hmi.print("${page}.${obj}.txt=${txt}");\n  hmi.write(hexEND,3);`);
    }  
    //% block="HMI串口智能屏 接收(R)管脚#[RXPIN] 发射(T)管脚#[TXPIN] 赋值[PAGE]的对象[OBJ]的整数[NUM]" blockType="command"
    //% RXPIN.shadow="dropdown" RXPIN.options="PIN_DigitalWrite" 
    //% TXPIN.shadow="dropdown" TXPIN.options="PIN_DigitalWrite"
    //% PAGE.shadow="normal" PAGE.defl="main" 
    //% OBJ.shadow="normal" OBJ.defl="n0" 
    //% NUM.shadow="number" NUM.defl="123"
    export function HMI_PAGE_NUM(parameter: any, block: any) {
        let rxpin = parameter.RXPIN.code;
        let txpin = parameter.TXPIN.code;
        let page = parameter.PAGE.code;
        let obj = parameter.OBJ.code;
        let num = parameter.NUM.code;
        Generator.addInclude('include_SoftwareSerial_h', '#include "SoftwareSerial.h"');
        Generator.addObject(`char_hexEND`, `char `,  `hexEND[3]={0xff,0xff,0xff};`);
        Generator.addObject(`SoftwareSerial_hmi`, `SoftwareSerial`,  `hmi(${rxpin},${txpin});`);
        Generator.addSetup(`hmi_begin`, `hmi.begin(9600);`);
        Generator.addCode(`hmi.print("${page}.${obj}.val=");\n  hmi.print(${num});\n  hmi.write(hexEND,3);`);
    }   
    //% block="HMI串口智能屏 接收(R)管脚#[RXPIN] 发射(T)管脚#[TXPIN] 赋值[PAGE]的对象[OBJ]的两位小数[XUM]" blockType="command"
    //% RXPIN.shadow="dropdown" RXPIN.options="PIN_DigitalWrite" 
    //% TXPIN.shadow="dropdown" TXPIN.options="PIN_DigitalWrite"
    //% PAGE.shadow="normal" PAGE.defl="main" 
    //% OBJ.shadow="normal" OBJ.defl="x0" 
    //% XUM.shadow="number" XUM.defl="3.14"
    export function HMI_PAGE_XUM(parameter: any, block: any) {
        let rxpin = parameter.RXPIN.code;
        let txpin = parameter.TXPIN.code;
        let page = parameter.PAGE.code;
        let obj = parameter.OBJ.code;
        let xum = parameter.XUM.code;
        Generator.addInclude('include_SoftwareSerial_h', '#include "SoftwareSerial.h"');
        Generator.addObject(`char_hexEND`, `char `,  `hexEND[3]={0xff,0xff,0xff};`);
        Generator.addObject(`SoftwareSerial_hmi`, `SoftwareSerial`,  `hmi(${rxpin},${txpin});`);
        Generator.addSetup(`hmi_begin`, `hmi.begin(9600);`);
        Generator.addCode(`hmi.print("${page}.${obj}.val=");\n  hmi.print((round((${xum} * 100))));\n  hmi.write(hexEND,3);`);
    } 

    //% block="HMI串口智能屏 接收(R)管脚#[RXPIN] 发射(T)管脚#[TXPIN] 有数据可读？" blockType="boolean"
    //% RXPIN.shadow="dropdown" RXPIN.options="PIN_DigitalWrite" 
    //% TXPIN.shadow="dropdown" TXPIN.options="PIN_DigitalWrite"
    export function HMI_AVAILABLE(parameter: any, block: any) {
        let rxpin = parameter.RXPIN.code;
        let txpin = parameter.TXPIN.code;
        Generator.addInclude('include_SoftwareSerial_h', '#include "SoftwareSerial.h"');
        Generator.addObject(`char_hexEND`, `char `,  `hexEND[3]={0xff,0xff,0xff};`);
        Generator.addObject(`SoftwareSerial_hmi`, `SoftwareSerial`,  `hmi(${rxpin},${txpin});`);
        Generator.addSetup(`hmi_begin`, `hmi.begin(9600);`);
        Generator.addCode(`hmi.available()`);
    } 
    //% block="HMI串口智能屏 接收(R)管脚#[RXPIN] 发射(T)管脚#[TXPIN] 读取原始数据" blockType="reporter"
    //% RXPIN.shadow="dropdown" RXPIN.options="PIN_DigitalWrite" 
    //% TXPIN.shadow="dropdown" TXPIN.options="PIN_DigitalWrite"
    export function HMI_READ(parameter: any, block: any) {
        let rxpin = parameter.RXPIN.code;
        let txpin = parameter.TXPIN.code;
        Generator.addInclude('include_SoftwareSerial_h', '#include "SoftwareSerial.h"');
        Generator.addObject(`char_hexEND`, `char `,  `hexEND[3]={0xff,0xff,0xff};`);
        Generator.addObject(`SoftwareSerial_hmi`, `SoftwareSerial`,  `hmi(${rxpin},${txpin});`);
        Generator.addSetup(`hmi_begin`, `hmi.begin(9600);`);
        Generator.addCode(`hmi.read()`);
    } 
}
